/**
 * Settings.java 
 *        
 * Linker is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package linker;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import linker.account.Account;
import linker.account.ManageAccount;

/**
 * This class is to hold settings for this user.
 * 
 * 
 */

public class Settings extends Object {

	/**
	 * 
	 */
	private Document set;

	/**
	 * Set path.
	 */
	private String setPath;

	/**
	 * 
	 */
	private static Settings settings;

	/**
	 * 
	 */
	private Settings() {
		String username = Account.getUser();
		username = username.replace("@", "at");
		username = username.replace(".", "-");
		SAXBuilder builder = new SAXBuilder();
		setPath = ManageAccount.getConfigPath() + File.separator + username
				+ ".xml";
		try {
			set = builder.build(new File(setPath));
		} catch (JDOMException e) {
			Element element = new Element("Prefs");
			set = new Document(element);
		} catch (IOException e) {
			Element element = new Element("Prefs");
			set = new Document(element);
		}
		if (getValue("localname") == null) {
			setValue("localname", username);
		}
	}

	/**
	 * @return The singlton setting.
	 */
	public static Settings getSettings() {
		if (settings == null) {
			settings = new Settings();
		}

		return settings;
	}

	/**
	 * 
	 * @param u
	 *            The Name of settings value.
	 * @return The boolean value.
	 */
	public final Boolean getBooleanValue(final String u) {
		return Boolean.valueOf(getValue(u));
	}

	/**
	 * Get the settings value by the given url.
	 * 
	 * @param u
	 *            The given url.
	 * @return The setting value.
	 */
	public final String getValue(final String u) {
		String name;
		String url = u;
		Element path = set.getRootElement();
		Element element;
		int firstPoint = url.indexOf(".");

		while (firstPoint > 0) {
			name = url.substring(0, firstPoint);

			element = path.getChild(name);
			if (element == null) {
				return null;
			} else {
				path = element;
			}

			url = url.substring(firstPoint + 1);
			firstPoint = url.indexOf(".");
		}

		element = path.getChild(url);
		if (element == null) {
			return null;
		} else {
			return element.getText();
		}
	}

	/**
	 * Change or add value by the given url.
	 * 
	 * @param u
	 *            The given url;
	 * @param value
	 *            The new value.
	 */
	public final void setValue(final String u, final String value) {
		String name;
		String url = u;
		Element path = set.getRootElement();
		Element element;
		int firstPoint = url.indexOf(".");

		while (firstPoint > 0) {
			name = url.substring(0, firstPoint);

			element = path.getChild(name);
			if (element == null) {
				Element add = new Element(name);
				path.addContent(add);
				path = add;
			} else {
				path = element;
			}

			url = url.substring(firstPoint + 1);
			firstPoint = url.indexOf(".");
		}

		element = path.getChild(url);
		if (element == null) {
			element = new Element(url);
			path.addContent(element);
		}

		element.setText(value);

		XMLOutputter outputer = new XMLOutputter(Format.getPrettyFormat());
		try {
			outputer.output(set, new FileOutputStream(setPath));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Delete the given url.
	 * 
	 * @param u
	 *            The given url.
	 */
	public final void deleteValue(final String u) {
		String name;
		String url = u;
		Element path = set.getRootElement();
		Element element;
		int firstPoint = url.indexOf(".");

		while (firstPoint > 0) {
			name = url.substring(0, firstPoint);

			element = path.getChild(name);
			if (element == null) {
				return;
			} else {
				path = element;
			}

			url = url.substring(firstPoint + 1);
			firstPoint = url.indexOf(".");
		}

		element = path.getChild(url);
		if (element == null) {
			return;
		}
		path.removeContent(element);

		XMLOutputter outputer = new XMLOutputter(Format.getPrettyFormat());
		try {
			outputer.output(set, new FileOutputStream(setPath));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int getElementSize(String u) {
		String name;
		String url = u;
		Element path = set.getRootElement();
		Element element;
		int firstPoint = url.indexOf(".");

		while (firstPoint > 0) {
			name = url.substring(0, firstPoint);

			element = path.getChild(name);
			if (element == null) {
				return 0;
			} else {
				path = element;
			}

			url = url.substring(firstPoint + 1);
			firstPoint = url.indexOf(".");
		}

		element = path.getChild(url);
		if (element == null) {
			return 0;
		} else {
			return element.getChildren().size();
		}
	}

}
